/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemadeleiloes.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import sistemadeleiloes.model.Leilao;
import sistemadeleiloes.model.Usuario;

/**
 *
 * @author Pedro
 */
public class ProdutoDAO {
    
        public int inserir(Leilao l, Usuario u) throws SQLException {
        Connection conn = Database.getConnection();
        String sql = "EXEC sp_InsProdutoLeilao ?,?,?,?,?,?,?,?,?";
        java.sql.Date dtInicio = new java.sql.Date(l.getDtInicio().getTime());
        java.sql.Date dtFinal = new java.sql.Date(l.getDtFinal().getTime());
        
        try {
            PreparedStatement pstm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstm.setString(1, l.getProduto().getNome());
            pstm.setString(2, l.getProduto().getDescricao());
            pstm.setDouble(3, l.getValorInicial());
            pstm.setDouble(4, l.getValorArremate());
            pstm.setDate(5, dtInicio);
            pstm.setDate(6, dtFinal);
            pstm.setString(7, l.getSenha());
            pstm.setInt(8, l.getStatus());
            pstm.setInt(9, u.getId());
            pstm.executeUpdate();   

            ResultSet rs = pstm.getGeneratedKeys();
            if(rs.next()){
                return rs.getInt(1);
             }
        } catch (SQLException e) {
            System.out.println("Erro ao inserir: " + e);
        }
        return 0;
    }
}
